<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\admin\service\FollowRecords as ServiceFollowRecords;
use app\facade\Basic;
use app\admin\service\Returnorder as ServiceReturnorder;
use think\Request;
use app\admin\validate\Returnorder as ValidateReturnorder;

/**
 *  退货退款单
 */
class Returnorder extends BaseController
{

    /**
     *   获取枚举
     */
    public function resource(Request $request)
    {
        $param['field'] = $request->param('field');
        $serviceReturnorder = new ServiceReturnorder();
        $result = $serviceReturnorder->getEnum($param);
        return Basic::json($result);
    }


    /**
     *  订单查询
     */
    public function getOrderList(Request $request)
    {
        $param['pageIndex'] = $request->param('pageIndex', 1);
        $param['pageSize'] = $request->param('pageSize', 10);
        $param['order_no'] = $request->param('order_no', null);
        $param['customer_id'] = $request->param('customer_id', null);
        $param['order_id'] = $request->param('order_id', null);

        /* 验证 */
        $this->validateData($param, 'Returnorder.getOrderList');

        $serviceReturnorder = new ServiceReturnorder();
        $result = $serviceReturnorder->getOrderList($param);
        return Basic::json($result);
    }


    /**
     *  退货退款单
     */
    public function getList(Request $request)
    {
        $param['pageIndex'] = $request->param('pageIndex', 1);
        $param['pageSize'] = $request->param('pageSize', 50);
        $param['returnorder_no'] = $request->param('returnorder_no', null);
        $param['customer_id'] = $request->param('customer_id', null);
        $param['signatory'] = $request->param('signatory', null);
        $param['return_reason'] = $request->param('return_reason', null);

        /* 产品和数量 */
        $param['product'] = $request->param('product', null);

        /* 退货退款单类型 */
        $param['returnorder_type'] = $request->param('returnorder_type', null);

        /* 签到开始时间 */
        $param['signing_start_date'] = $request->param('signing_start_date', null);
        $param['signing_end_date'] = $request->param('signing_end_date', null);

        /* 退货总金额  */
        $param['min_total_amount'] = $request->param('min_total_amount', null);
        $param['max_total_amount'] = $request->param('max_total_amount', null);

        /* 已退款金额 */
        $param['min_paid_amount'] = $request->param('min_paid_amount', null);
        $param['max_paid_amount'] = $request->param('max_paid_amount', null);


        /* 验证 */
        $this->validateData($param, 'Returnorder.getList');

        $serviceReturnorder = new ServiceReturnorder();
        $result = $serviceReturnorder->getList($param);
        return Basic::json($result);
    }


    /**
     *  退货单
     */
    public function getReturnOrderList(Request $request)
    {
        $param['pageIndex'] = $request->param('pageIndex', 1);
        $param['pageSize'] = $request->param('pageSize', 50);
        $param['returnorder_no'] = $request->param('returnorder_no', null);
        $param['customer_id'] = $request->param('customer_id', null);
        $param['signatory'] = $request->param('signatory', null);
        $param['return_reason'] = $request->param('return_reason', null);

        /* 产品和数量 */
        $param['product'] = $request->param('product', null);

        /* 退货退款单类型 */
        $param['returnorder_type'] = 3;

        /* 签到开始时间 */
        $param['signing_start_date'] = $request->param('signing_start_date', null);
        $param['signing_end_date'] = $request->param('signing_end_date', null);

        /* 退货总金额  */
        $param['min_total_amount'] = $request->param('min_total_amount', null);
        $param['max_total_amount'] = $request->param('max_total_amount', null);

        /* 已退款金额 */
        $param['min_paid_amount'] = $request->param('min_paid_amount', null);
        $param['max_paid_amount'] = $request->param('max_paid_amount', null);




        /* 验证 */
        $this->validateData($param, 'Returnorder.getList');

        $serviceReturnorder = new ServiceReturnorder();
        $result = $serviceReturnorder->getList($param, 2);
        return Basic::json($result);
    }

    /**
     *  退款单
     */
    public function getRefundOrderList(Request $request)
    {
        $param['pageIndex'] = $request->param('pageIndex', 1);
        $param['pageSize'] = $request->param('pageSize', 50);
        $param['status'] = $request->param('status', null);
        $param['transfer_type'] = $request->param('transfer_type', null);
        /* 验证 */
        // $this->validateData($param, 'Returnorder.getList');

        $serviceReturnorder = new ServiceReturnorder();
        $result = $serviceReturnorder->getRefundOrderList($param);
        return Basic::json($result);
    }

    /**
     *  操作记录
     */
    public function getOperationRecords(Request $request)
    {
        $param['pageIndex'] = $request->param('pageIndex', 1);
        $param['pageSize'] = $request->param('pageSize', 100);
        $param['returnorder_id'] = $request->param('returnorder_id', 0);

        /* 验证 */
        $this->validateData($param, 'Returnorder.getOperationRecords');

        $serviceReturnorder = new ServiceReturnorder();
        $result = $serviceReturnorder->getOperationRecords((int)$param['pageIndex'], (int)$param['pageSize'], (int)$param['returnorder_id'], 'id,user_id,operate,user_type,content,create_time');
        return Basic::json($result);
    }


    /**
     *  添加
     */
    public function save(Request $request)
    {
        $param['creator'] = $request->operator_id;
        $param['returnorder_type'] = $request->param('returnorder_type', null);
        $param['customer_id'] = $request->param('customer_id', null);
        $param['order_id'] = $request->param('order_id', null);
        $param['products_list'] = $request->param('products_list', []);
        $param['signatory'] = $request->param('signatory', null);
        $param['signing_date'] = $request->param('signing_date', null);
        $param['owner'] = $request->param('owner', null);
        $param['partner'] = $request->param('partner', []);
        $param['return_reason'] = $request->param('return_reason', null);
        $param['remark'] = $request->param('remark', null);
        $param['payment_list'] = $request->param('payment_list', null);
        $param['returnorder_method'] = $request->param('returnorder_method', null);
        $param['delivery_address'] = $request->param('delivery_address', null);
        $param['tracking_number'] = $request->param('tracking_number', null);
        $param['express_company'] = $request->param('express_company', null);
        $param['attachfiles'] = $request->param('attachfiles', null);
        $param['payment_address'] = $request->param('payment_address', null);

        /* 验证 */
        $this->validateData($param, 'Returnorder.save');

        /* 自定义验证 */
        $validate = new ValidateReturnorder();
        $result = $validate->customValidationRules($param);
        if ($result !== true) {
            return Basic::json($result);
        }

        $serviceReturnorder = new ServiceReturnorder();
        $result = $serviceReturnorder->save($param);
        return Basic::json($result);
    }


    /**
     *  获取详情
     */
    public function detail(Request $request)
    {
        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        /* 验证 */
        $this->validateData($param, 'Returnorder.detail');

        $serviceReturnorder = new ServiceReturnorder();
        $result = $serviceReturnorder->detail($param);
        return Basic::json($result);
    }

    /**
     *  修改
     */
    public function update(Request $request)
    {
        $param['id'] = $request->param('id', null);
        $param['creator'] = $request->operator_id;
        $param['returnorder_type'] = $request->param('returnorder_type', null);
        $param['customer_id'] = $request->param('customer_id', null);
        $param['order_id'] = $request->param('order_id', null);
        $param['products_list'] = $request->param('products_list', []);
        $param['signatory'] = $request->param('signatory', null);
        $param['signing_date'] = $request->param('signing_date', null);
        $param['owner'] = $request->param('owner', null);
        $param['partner'] = $request->param('partner', []);
        $param['return_reason'] = $request->param('return_reason', null);
        $param['remark'] = $request->param('remark', null);
        $param['payment_list'] = $request->param('payment_list', null);
        $param['returnorder_method'] = $request->param('returnorder_method', null);
        $param['delivery_address'] = $request->param('delivery_address', null);
        $param['tracking_number'] = $request->param('tracking_number', null);
        $param['express_company'] = $request->param('express_company', null);
        $param['attachfiles'] = $request->param('attachfiles', null);
        $param['payment_address'] = $request->param('payment_address', null);

        /* 验证 */
        $this->validateData($param, 'Returnorder.save');

        /* 自定义验证 */
        $validate = new ValidateReturnorder();
        $result = $validate->customValidationRules($param);
        if ($result !== true) {
            return Basic::json($result);
        }


        $serviceReturnorder = new ServiceReturnorder();
        $result = $serviceReturnorder->update($param);
        return Basic::json($result);
    }


    /**
     *  退款单支付
     */
    public function returnorderPayment(Request $request)
    {
        $param['creator'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        $param['voucher_id'] = $request->param('voucher_id', null);
        $param['amount'] = $request->param('amount', null);
        $param['pay_currency'] = $request->param('pay_currency', 214);
        $param['notes'] = $request->param('notes', null);

        /* 验证 */
        $this->validateData($param, 'Returnorder.returnorderPayment');

        $serviceReturnorder = new ServiceReturnorder();
        $result = $serviceReturnorder->returnorderPayment($param);
        return Basic::json($result);
    }


    /**
     *  取消退款单
     */
    public function cancel(Request $request)
    {
        $param['creator'] = $request->operator_id;
        $param['id'] = $request->param('id', null);

        /* 验证 */
        $this->validateData($param, 'Returnorder.cancel');

        $serviceReturnorder = new ServiceReturnorder();
        $result = $serviceReturnorder->cancel($param);
        return Basic::json($result);
    }


    /**
     *  退款单审核
     */
    public function review(Request $request)
    {
        $param['creator'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        $param['status'] = $request->param('status', null);

        /* 验证 */
        $this->validateData($param, 'Returnorder.cancel');

        $serviceReturnorder = new ServiceReturnorder();
        $result = $serviceReturnorder->review($param);
        return Basic::json($result);
    }
    /**
     * @describe 退货退款单退货入库
     * @author: Bobo
     * @date: 2025-08-08 13:11:57
     * @param [type] @required
     * @return Renderable
     */

    public function stock(Request $request)
    {
        $param['id'] = $request->param('id', null);
        $param['stock_list'] = $request->param('stock_list', null);
        $param['creator'] = $request->operator_id;

        /* 验证 */
        $this->validateData($param, 'Returnorder.stock');

        $serviceReturnorder = new ServiceReturnorder();
        $result = $serviceReturnorder->stock($param);
        return Basic::json($result);
    }
}
